package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/FindAll")
    public Result FindAll(){
        List<Role> roleList = roleService.findallrole();
        if (roleList != null && roleList.size() > 0) {
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleList);
        }

        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    //   新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.add(role, permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            //如果新增失败
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
//        新增成功
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    //    分页查询
    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    //回显角色信息 根据当前角色Id
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //回显权限信息
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> integerList = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, integerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    //回显权限信息
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> integerList = roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, integerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //   编辑检查组
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.edit(role, permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            //如果新增失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
//        新增成功
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //删除
    @RequestMapping("/del")
    public Result del(Integer id) {
        try {
            roleService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }

/*    @RequestMapping("/FindAll")
    public Result find() {
        List<Role> groupList = roleService.findallitem();
        if (groupList != null && groupList.size() > 0) {
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, groupList);
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }*/


}
